//
//  UserManager.swift
//  DYSwiftFramework
//
//  Created by huangdeyu on 2021/10/28.
//  Copyright © 2021 none. All rights reserved.
//

import Foundation
import SwiftyJSON


private let USER_INFO_KEY = "USER_INFO_KEY"


let USER = UserManager.shared

struct UserInfo {
    var avatar:String?
    var name:String?
    var ticket:String? = "189825234_f7d41b7af52beef1b36b7461f8425a86"
    init(_ dic:[String:Any?]?) {
        avatar = dic?["avatar"] as? String
        name = dic?["name"] as? String
//        ticket = dic?["ticket"] as? String
    }
}

class UserManager {
    static let shared = UserManager()
    private init() {}
    
    
    var isLogin:Bool {
        return userInfo?.ticket != nil
    }
    
    var ticket:String? {
        return userInfo?.ticket
    }
    
    private var _userInfo:UserInfo?
    var userInfo:UserInfo? {
        if let u = _userInfo {
            return u
        }
        let userInfoDic = UserDefaults.standard.dictionary(forKey: USER_INFO_KEY)
        _userInfo = UserInfo(userInfoDic)
        return _userInfo
    }
    
    func login(name:String,pwd:String,result:(Bool)->Void) {
        
    }
    
    // 因为电脑不是最新的系统，无法使用async 和 await 只能先用回调的形式
    func login(phone:String,code:String,result:(Bool)->Void)  {
        
    }
    
    func updateUserInfo() {
        
    }
}
